Learn R Programming

bnlearn (version 1.1)

arc operations: Drop, add or set the direction of an arc

Description

Drop, add or set the direction of an arc.

Usage

set.arc(x, from, to, check.cycles = TRUE, debug = FALSE)
  drop.arc(x, from, to, debug = FALSE)
  reverse.arc(x, from, to, check.cycles = TRUE, debug = FALSE)

Arguments

x
an object of class bn.
from
a character string, the label of a node.
to
a character string, the label of another node.
check.cycles
a boolean value. If TRUE the graph is tested for acyclicity; otherwise the graph is returned anyway.
debug
a boolean value. If TRUE a lot of debugging output is printed; otherwise the function is completely silent.

Value

  • set.arc and drop.arc return invisibly an updated copy of x.

Details

The set.arc function operates in the following way:
  • if there is no arc betweenfromandto, the arc (from,to) is added.
  • if there is an undirected arc betweenfromandto, its direction is set to (from,to).
  • if the arc (to,from) is present, it's reversed.
  • if the arc (from,to) is present, no action is taken.

The drop.arc function operates in the following way:

  • if there is no arc betweenfromandto, no action is taken.
  • if there is an undirected arc betweenfromandto, it's dropped.
  • if there is a directed arc betweenfromandto, it's dropped regardless of its direction.

The reverse.arc function operates in the following way:

  • if there is no arc betweenfromandto, it returns an error.
  • if there is an undirected arc betweenfromandto, it returns an error.
  • if the arc (to,from) is present, it's reversed.
  • if the arc (from,to) is present, it's reversed.

Examples

Run this code
data(learning.test)

res = gs(learning.test)
res2 = set.arc(res, "A", "B")
compare(res, res2, debug = TRUE)
# * children of A in r1 not present in r2:
# character(0)
# * children of A in r2 not present in r1:
# [1] "B"
# * parents of B in r1 not present in r2:
# character(0)
# * parents of B in r2 not present in r1:
# [1] "A"
# * directed arcs in r1 not present in r2:
# character(0)
# * directed arcs in r2 not present in r1:
# [1] "A -> B"
# * undirected arcs in r1 not present in r2:
# [1] "A - B" "B - A"
# * undirected arcs in r2 not present in r1:
# character(0)
# [1] FALSE
res2 = drop.arc(res, "A", "B")
compare(res, res2, debug = TRUE)
# * nodes in the markov blanket of A in r1 not present in r2:
# [1] "B"
# * nodes in the markov blanket of A in r2 not present in r1:
# character(0)
# * nodes in the neighbourhood of A in r1 not present in r2:
# [1] "B"
# * nodes in the neighbourhood of A in r2 not present in r1:
# character(0)
# * nodes in the markov blanket of B in r1 not present in r2:
# [1] "A"
# * nodes in the markov blanket of B in r2 not present in r1:
# character(0)
# * nodes in the neighbourhood of B in r1 not present in r2:
# [1] "A"
# * nodes in the neighbourhood of B in r2 not present in r1:
# character(0)
# * undirected arcs in r1 not present in r2:
# [1] "A - B" "B - A"
# * undirected arcs in r2 not present in r1:
# character(0)
# [1] FALSE
res2 = reverse.arc(res, "A", "D")
compare(res, res2, debug = TRUE)
# * nodes in the markov blanket of A in r1 not present in r2:
# [1] "C"
# * nodes in the markov blanket of A in r2 not present in r1:
# character(0)
# * parents of A in r1 not present in r2:
# character(0)
# * parents of A in r2 not present in r1:
# [1] "D"
# * children of A in r1 not present in r2:
# [1] "D"
# * children of A in r2 not present in r1:
# character(0)
# * nodes in the markov blanket of C in r1 not present in r2:
# [1] "A"
# * nodes in the markov blanket of C in r2 not present in r1:
# character(0)
# * parents of D in r1 not present in r2:
# [1] "A"
# * parents of D in r2 not present in r1:
# character(0)
# * children of D in r1 not present in r2:
# character(0)
# * children of D in r2 not present in r1:
# [1] "A"
# * directed arcs in r1 not present in r2:
# [1] "A -> D"
# * directed arcs in r2 not present in r1:
# [1] "D -> A"
# [1] FALSE

## use debug = TRUE for more information
set.arc(res, "A", "B", debug = TRUE)
drop.arc(res, "A", "B", debug = TRUE)
reverse.arc(res, "A", "D", debug = TRUE)

Run the code above in your browser using DataLab